/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pruebahttpsoneway;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;

/**
 *
 * @author icardenas
 */
public class PruebaHTTPSOneWay {

   /**
    * @param args the command line arguments
    */
   public static void main(String[] args) throws NoSuchAlgorithmException {
      try {
         // Carga del fichero que tiene los certificados de los servidores en
         // los que confiamos.
         InputStream fileCertificadosConfianza = new FileInputStream(new File(
               ".keystore"));
         KeyStore ksCertificadosConfianza = KeyStore.getInstance(KeyStore
               .getDefaultType());
         ksCertificadosConfianza.load(fileCertificadosConfianza,
               "ivan123".toCharArray());
         fileCertificadosConfianza.close();

         // Ponemos el contenido en nuestro manager de certificados de
         // confianza.
         TrustManagerFactory tmf = TrustManagerFactory
               .getInstance(TrustManagerFactory.getDefaultAlgorithm());
         tmf.init(ksCertificadosConfianza);

         // Creamos un contexto SSL con nuestro manager de certificados en los
         // que confiamos.
         SSLContext context = SSLContext.getInstance("TLS");
         context.init(null, tmf.getTrustManagers(), null);
         SSLSocketFactory sslSocketFactory = context.getSocketFactory();

         // Abrimos la conexión y le pasamos nuestro contexto SSL
         URL url = new URL("https://control.unet.edu.ve/imagenes/FotosE/V00178755901.jpg");
         URLConnection conexion = url.openConnection();
         ((HttpsURLConnection) conexion).setSSLSocketFactory(sslSocketFactory);

         // Ya podemos conectar y leer
         conexion.connect();
         InputStream is = conexion.getInputStream();
         BufferedReader br = new BufferedReader(new InputStreamReader(is));
         char[] buffer = new char[1000];
         int leido;
         OutputStream os = new FileOutputStream("Imagen.jpg");
         
         byte[] b = new byte[2048];
		int length;

		while ((length = is.read(b)) != -1) {
			os.write(b, 0, length);
		}

		is.close();
		os.close();
      }  catch (FileNotFoundException e){
         e.printStackTrace();
         
            
      } catch (MalformedURLException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (KeyStoreException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (NoSuchAlgorithmException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (CertificateException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (KeyManagementException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } 
   }
}
